<template>
  <el-dialog v-model="dialogVisible" title="修改用户信息" width="400px">
    <el-form :model="selectedUser">
      <el-form-item label="姓名">
        <el-input v-model="selectedUser.name"></el-input>
      </el-form-item>
      <el-form-item label="年龄">
        <el-input type="number" v-model="selectedUser.age"></el-input>
      </el-form-item>
      <el-form-item label="邮件">
        <el-input v-model="selectedUser.email"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" @click="modifyUser">提交</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, defineProps, defineEmits } from 'vue';
import { updateUser } from '@/alova/api';

const dialogVisible = ref(false);
const selectedUser = ref({ id: '', name: '', age: '', email: '' });
const emit = defineEmits(['userModified']);

// 修改用户信息方法
const modifyUser = async () => {
  try {
    await updateUser(selectedUser.value);
    dialogVisible.value = false;
    emit('userModified'); // 通知父组件刷新数据
  } catch (error) {
    console.error('修改失败', error);
  }
};

// 接收选中的用户信息
const openDialog = (user) => {
  selectedUser.value = { ...user };
  // sselectedUser.value = user;
  dialogVisible.value = true;
};

defineExpose({ openDialog });
</script>

<style>
.error {
  color: red;
}
</style>
